package com.gs.api.util.video.parse.weishi;

import com.alibaba.fastjson.JSONObject;
import com.gs.api.util.UrlParamUtil;
import com.gs.api.util.video.parse.ParseConstant;
import com.gs.api.util.video.parse.vo.VideoVo;
import org.jsoup.Jsoup;

import java.util.Map;

/**
 * @author guoyunfei on 2022/4/14 16:07
 * @className WeishiApp
 * @description TODO 微视
 * @Version 1.0
 */
public class WeishiApp {

    /**
     * 微视视频
     * @param url
     */
    public static VideoVo weishiParseUrl(String url) {

        VideoVo videoVo = new VideoVo();

        Map<String, String> params = UrlParamUtil.getParams(url);

        String videoId = null;

        for(String key : params.keySet()){
            if("id".equals(key)){
                String value = params.get(key).toString();
                videoId = value;
                break;
            }
        }

        if(null == videoId){
            return null;
        }

        try {
            String jsonStr = Jsoup.connect(ParseConstant.weiShiParseUrl + videoId).ignoreContentType(true).execute().body();

            JSONObject obj = JSONObject.parseObject(jsonStr);
            JSONObject data = obj.getJSONObject("data");

            JSONObject feeds = data.getJSONArray("feeds").getJSONObject(0);

            //类型
            //String type = feeds.getString("type");
            videoVo.setType(ParseConstant.PARSE_VIDEO);
            //原视频URL
            String video_url = feeds.getString("video_url");
            videoVo.setVideoSrc(video_url);
            //封面图
            //String image = feeds.getJSONObject("video_cover").getJSONObject("static_cover").getString("url");
            String image = feeds.getJSONArray("images").getJSONObject(0).getString("url");
            videoVo.setCoverSrc(image);
            //文案
            String feed_desc = feeds.getString("feed_desc");
            videoVo.setCopywriting(feed_desc);
            String mp3 = feeds.getJSONObject("music_info").getJSONObject("songInfo").getString("strPlayUrl");
            videoVo.setAudioSrc(mp3);

        }catch (Exception e){
            e.printStackTrace();
            videoVo.setStatus(500);
        }

        return videoVo;
    }

}
